/* 
更新search模块的state数据
*/
import { AnyAction } from "redux"
import { SearchState } from "./types"
// 初始state
const initState: SearchState = {
  firstView: true,
  loading: false,
  errMsg: "",
  users: [],
}

// 注意不要直接修改state内部数据, 而要产生一个新的
export default function search(state = initState, action: AnyAction) {
  switch (action.type) {
    case "REQUESTING": // 请求中
      return {
        firstView: false,
        loading: true,
        errMsg: "",
        users: [],
      }
    case "REQ_SUCCESS": // 请求成功
      return {
        ...state,
        loading: false,
        users: action.data,
      }
    case "REQ_ERROR": // 请求失败
      return {
        ...state,
        loading: false,
        errMsg: action.data,
      }

    default:
      return state
  }
}
